package com.soup.lab.validation;

import com.soup.lab.validation.model.ArrayModel;
import com.soup.lab.validation.model.GenericModel;
import com.soup.lab.validation.model.SimpleModel;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/")
@Validated(ValidController.A.class)
public class ValidController {

    @PostMapping("simple2")
    public Object simple2(@Validated SimpleModel simpleModel) {
        return simpleModel;
    }

    @PostMapping("simple3")
    @Validated(ValidController.B.class)
    public @NotNull Object simple3(@Max(value = 10, groups = B.class) Integer age,
                                   @NotEmpty(groups = B.class) String name) {
        return age;
    }

    public interface A{}
    public interface B{}


    @PostMapping("simple")
    public Object simple(@Valid @RequestBody SimpleModel simpleModel) {
        return simpleModel;
    }

    @PostMapping("special")
    public Object special(@Validated(SimpleModel.Special.class) @RequestBody SimpleModel simpleModel) {
        return simpleModel;
    }

    @PostMapping("array")
    public Object array(@Valid @RequestBody ArrayModel arrayModel) {
        return arrayModel;
    }

    @PostMapping("generic")
    public Object generic(@Valid @RequestBody GenericModel<SimpleModel> genericModel) {
        return genericModel;
    }
    @PostMapping("genericArray")
    public Object genericArray(@Valid @RequestBody GenericModel<List<SimpleModel>> genericModel) {
        return genericModel;
    }
    @PostMapping("genericMap")
    public Object genericMap(@Valid @RequestBody GenericModel<Map<String, SimpleModel>> genericModel) {
        return genericModel;
    }

//    @ExceptionHandler({MethodArgumentNotValidException.class})
//    public String handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
//        List<ObjectError> errors = e.getBindingResult().getAllErrors();
//        return errors.get(0).getDefaultMessage();
//    }
}
